package com.project2.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.bo.BrandCreateBo;
import com.project2.bean.bo.BrandUpdateBo;
import com.project2.bean.po.MarketBrand;
import com.project2.bean.po.MarketBrandExample;
import com.project2.bean.vo.DataInAdminBrandVo;
import com.project2.bean.vo.DataInBrandUpdateVo;
import com.project2.bean.vo.ListInDataInAdminBrandVo;
import com.project2.mapper.MarketBrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangminghui
 * @date 2022/8/13
 */
@Service
public class AdminBrandServiceImpl implements AdminBrandService {
    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Override
    public CommonData<ListInDataInAdminBrandVo> queryAllBrandInfo(Integer id, String name, PageBo pageBo) {
        // todo: 传入的id，name都为空(请求参数没有携带)，显示全部数据
        //  传入id正确，name为空或不为空，显示传入id对应的商品品牌
        //  传入id不正确，name为空或不为空，都查不到数据
        //  传入id为空，name为空，根据name做模糊查询
        //  id的优先级要高于name
        //  还要判断一下deleted字段是true还是false，是true就不显示
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        MarketBrandExample example = new MarketBrandExample();
        if (pageBo.getSort() != null && pageBo.getOrder() != null) {
            example.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        }
        MarketBrandExample.Criteria criteria = example.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id).andDeletedEqualTo(false);
        }
        // 多加一个id==null判断
        if (id == null && name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%").andDeletedEqualTo(false);
        }
        // 走到这里，说明传入的id、name都为空，example应为null
        criteria.andDeletedEqualTo(false);
        List<MarketBrand> marketBrandList = marketBrandMapper.selectByExample(example);
//        ArrayList<MarketBrand> brands = new ArrayList<>();
//        for (MarketBrand marketBrand : marketBrandList) {
//            if (marketBrand.getDeleted() == false) {
//                brands.add(marketBrand);
//            }
//        }
        PageInfo<MarketBrand> pageInfo = new PageInfo<>(marketBrandList);
        //PageInfo<MarketBrand> pageInfo = new PageInfo<>(brands);
        return CommonData.data(pageInfo);
    }

    @Override
    public void addBrandInfo(BrandCreateBo brandCreateBo) {
        Date date = new Date();
        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setName(brandCreateBo.getName());
        marketBrand.setDesc(brandCreateBo.getDesc());
        marketBrand.setPicUrl(brandCreateBo.getPicUrl());
        marketBrand.setAddTime(date);
        marketBrand.setUpdateTime(date);
        marketBrand.setFloorPrice(new BigDecimal(brandCreateBo.getFloorPrice()));
        marketBrandMapper.insertSelective(marketBrand);
    }

    @Override
    public DataInAdminBrandVo queryNewAddBrandInfoByNameAndDesc(String name, String desc) {
        List<DataInAdminBrandVo> dataInAdminBrandVos = marketBrandMapper.queryBrandInfoByNameAndDesc(name, desc);
        // 添加时间越大的，在数据库越后面
        return dataInAdminBrandVos.get(dataInAdminBrandVos.size() - 1);
    }

    @Override
    public int updateBrandInfo(BrandUpdateBo brandUpdateBo) {
        Date date = new Date();
        MarketBrand marketBrand = new MarketBrand();
        // todo: 有毒。。。
        //marketBrand.setId(marketBrand.getId());
        marketBrand.setId(brandUpdateBo.getId());
        marketBrand.setName(brandUpdateBo.getName());
        marketBrand.setDesc(brandUpdateBo.getDesc());
        marketBrand.setPicUrl(brandUpdateBo.getPicUrl());
        marketBrand.setFloorPrice(new BigDecimal(brandUpdateBo.getFloorPrice()));
        marketBrand.setAddTime(date);
        marketBrand.setUpdateTime(date);
        int count = marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
        return count;
    }

    @Override
    public DataInBrandUpdateVo queryUpdateBrandInfoById(Integer id) {
        DataInBrandUpdateVo dataInBrandUpdateVo = marketBrandMapper.selectUpdateBrandInfoById(id);
        return dataInBrandUpdateVo;
    }

    @Override
    public void deleteBrandInfoById(Integer id) {
        MarketBrand marketBrand = new MarketBrand();
        marketBrand.setId(id);
        marketBrand.setDeleted(true);
        marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
    }


}
